package com.lhd.ivos.user.controller;

/**
 * @author lhd
 * date 2024/5/21
 * @apiNote
 */

import com.lhd.ivos.base.response.JsonResult;
import com.lhd.ivos.base.response.StatusCode;
import com.lhd.ivos.user.pojo.dto.UserLoginParam;
import com.lhd.ivos.user.pojo.dto.UserQuery;
import com.lhd.ivos.user.pojo.dto.UserSaveParam;
import com.lhd.ivos.user.pojo.vo.UserVO;
import com.lhd.ivos.user.service.UserService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 用户登录
     * POST http://localhost:8080/v1/user/login?username=admin&password=admin
     * @param userLoginParam
     * @return
     */
    @PostMapping("login")
    public JsonResult login( UserLoginParam userLoginParam){
        UserVO userVO = userService.login(userLoginParam);
        return JsonResult.ok(userVO);
    }


    /**
     * 保存用户信息的方法
     *
     * @param userSaveParam 用户保存参数，包含要保存的用户信息
     * @return JsonResult 返回操作结果，如果保存成功返回OK，否则返回操作失败的状态码
     */
    @PostMapping("save")
    public JsonResult save( UserSaveParam userSaveParam){
        log.info("userSaveParam:{}",userSaveParam);
        // 调用UserService的save方法保存用户信息
        int result = userService.save(userSaveParam);
        if (result > 0){
            // 如果保存成功，返回成功结果
            return JsonResult.ok();
        }
        // 如果保存失败，返回失败结果
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @GetMapping("select")
    public JsonResult selectUsers(UserQuery userQuery){
        log.info("查询用户:userQuery{}",userQuery);
        List<UserVO> list = userService.selectUsers(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("update/password/{userId}")
    public JsonResult updatePassword(@PathVariable Long userId){
        log.debug("userId = {}", userId);
        userService.updatePassword(userId);
        return JsonResult.ok();
    }


    @PostMapping("update/status/{userId}/{status}")
    public JsonResult updateUserStatus(@PathVariable Long userId, @PathVariable String status) {
        log.info("更新状态 userId = {}, status = {}", userId, status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId ){
        log.debug("删除用户 userId = {}", userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }

    @GetMapping("select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId){
        log.debug("查询上级审批人列表 parentId = {}", parentId);
        List<UserVO> list = userService.selectAuditByParentId(parentId);
        return JsonResult.ok(list);
    }

}